package com.douqu.game.battle.util;

import com.douqu.game.core.astar.*;
import com.douqu.game.core.config.common.Position;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.util.LogUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Test
{


    public static void main(String[] args)
    {
        ExecutorService service = Executors.newSingleThreadExecutor();
        try {
            service.awaitTermination(5000, TimeUnit.MILLISECONDS);//设置超时时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i = 0; i < 5; i++)
        {
            service.execute(() -> {
                startTime = System.currentTimeMillis();
                LogUtils.info("线程开始 -> " + Thread.currentThread().getName() + " time -> " + startTime);
                LogUtils.info("开始休息");
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LogUtils.info("休息完成");
            });
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                LogUtils.info(Thread.currentThread().getName() + "  检测");
                while(true)
                {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        if(service.awaitTermination(5, TimeUnit.SECONDS))
                        {
                            LogUtils.info("寻路超过5秒还没回应");
                            service.shutdown();
    //                        LogUtils.info(service.getClass());
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();

    }

    private static long startTime;


    private static void testDG()
    {

    }

    private static int testDDG(int count)
    {
        return count+1;
    }




    public static void test1()
    {
        //自己的位置
        int selfX = 4;
        int selfY = 5;
        //召唤主的占位
        int selfArea = 2;
        //召唤出来的兵的占位
        int targetArea = 2;
        int teamNo = 1;
        int width = 50;
        int needCount = 3;
        int middleObstacleMinX = 21;
        int middleObstacleMaxX = 28;

        boolean left = teamNo == ConstantFactory.BATTLE_TEAM_1 ? selfX > targetArea + selfArea : selfX > width - selfArea - targetArea;

        AstarMap info = new AstarMap(AstarMap.AnchorPos.BottomLeft, BattleUtils.BATTLE_MAP_1);

        AStar astar = new AStar(info);

        List<Position> result = new CopyOnWriteArrayList<>();
        if(left)
        {
            //放在左边
            choose:
            for(int i = selfX - 1; i > 0; i--)
            {
                if(i <= middleObstacleMaxX && i >= middleObstacleMinX)
                    continue;
                for(int j = selfY + selfArea; j > 0; j--)
                {
                    if(j < targetArea)//不够位置放下要召唤的兵了
                        break;

                    if(!astar.getAstarMap().isObstacleToApplication(i, j))
                    {
                        result.add(new Position(i, j));
                        if(result.size() >= needCount)
                            break choose;
                    }
                }
            }
            if(result.size() < needCount)
            {
                choose:
                for(int i = selfX + 1; i < width-1; i++)
                {
                    if(i <= middleObstacleMaxX && i >= middleObstacleMinX)
                        continue;

                    for(int j = selfY + selfArea; j > 0; j--)
                    {
                        if(j < targetArea)//不够位置放下要召唤的兵了
                            break;

                        if(!astar.getAstarMap().isObstacleToApplication(i, j))
                        {
                            result.add(new Position(i, j));
                            if(result.size() >= needCount)
                                break choose;
                        }
                    }
                }
            }
        }
        else
        {
            //放在右边
        }
    }

    public static void testRoute()
    {
        //二维数组地图以左上对齐方式
        AstarMap info = new AstarMap(AstarMap.AnchorPos.BottomLeft, BattleUtils.BATTLE_MAP_1);

        AStar astar = new AStar(info);

        Position master1 = new Position(3,12);
        ObjectBound targetBound = new ObjectBound(3);
        AbsCoord coord = new AbsCoord((float)(master1.x+Math.random()),(float)(master1.y+Math.random()));
        targetBound.setCenter(coord);
        astar.getAstarMap().setMap(targetBound, AstarMap.MapSignal.Player);

        Position master2 = new Position(46,12);
        targetBound = new ObjectBound(3);
        coord = new AbsCoord((float)(master2.x+Math.random()),(float)(master2.y+Math.random()));
        targetBound.setCenter(coord);
        astar.getAstarMap().setMap(targetBound, AstarMap.MapSignal.Player);

        Position monster = new Position(1, 6);
        targetBound = new ObjectBound(4);
        coord = new AbsCoord((float)(monster.x+Math.random()),(float)(monster.y+Math.random()));
        targetBound.setCenter(coord);
        astar.getAstarMap().setMap(targetBound, AstarMap.MapSignal.Player);

        System.out.println("------------------------------------------------------------");
        info.setMap(8,2, AstarMap.MapSignal.Player);
        info.setMap(5,2, AstarMap.MapSignal.Player);
//        info.setMap(5,3, AstarMap.MapSignal.Player);
//        info.setMap(6,2, AstarMap.MapSignal.Player);
//        info.setMap(6,3, AstarMap.MapSignal.Player);
//        info.setMap(20,2, AstarMap.MapSignal.Player);
//        info.setMap(33,2, AstarMap.MapSignal.Player);
//        info.setMap(33,3, AstarMap.MapSignal.Player);
//        info.setMap(34,2, AstarMap.MapSignal.Player);
//        info.setMap(34,3, AstarMap.MapSignal.Player);
        System.out.println(info);


//        for(int i = 0; i < 1; i++)
//        {
//            int startX = 30, startY = 2, endX = 3, endY = 12;
//
//            search(astar, new Node(startX, startY), new Node(endX, endY));
//        }

        astar.getAstarMap().setMap(targetBound, AstarMap.MapSignal.Free);

        System.out.println(info);
    }


    public static void search(AStar astar, Node start, Node end)
    {
        final long t1 = System.currentTimeMillis();
        System.out.println("起点 -> " + start.grid + "  终点 -> " + end.grid);
//        astar.getPath(start, end, pathNodes -> {
////				for (Node n : pathNodes)
////				{
////					System.out.print("node:"+n.grid.x+ " 	"+n.grid.y+"\n");
////				}
////				List<ObjectBound> players = new  ArrayList<ObjectBound>();
////				ObjectBound playerOne = new ObjectBound(2);
////				players.add(playerOne);
////				Tick(players,pathNodes);
//				System.out.print("===============================================\n");
////				printMap(astar.getAstarMap().maps);
//            final long t2 = System.currentTimeMillis();
//            System.out.println("获取路线花费时间:" + (t2 - t1));
//            System.out.println(astar.getAstarMap());
//            System.out.println("获取到路线:" + pathNodes);
//        });
    }

    public static void Tick(final List<ObjectBound> players,final List<Node> pathNodes)
    {
        final long timeInterval = 500;
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                int index = 0;
                while (index < pathNodes.size())
                {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    System.out.println(df.format(new Date()));
                    try
                    {
                        Node curNode = pathNodes.get(index);
                        AbsCoord coord = new AbsCoord((float)(curNode.grid.x+Math.random()),(float)(curNode.grid.y+Math.random()));
                        players.get(0).setCenter(coord);
//                        MapManager.setMapPlayers(players, MapManager.MapSignal.Player);
                        System.out.print("source center pos:"+players.get(0).center.toString()+" \n");
//						printMap(MapManager.flexibleMapInfo.maps);
                        index++;
                        Thread.sleep(timeInterval);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
    /**
     * 打印地图
     */
    public static void printMap(int[][] maps)
    {
        for (int i = 0; i < maps.length; i++)
        {
            for (int j = 0; j < maps[i].length; j++)
            {
                System.out.print(maps[i][j] + " ");
            }
            System.out.println();
        }
    }
}
